/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-08-31 19:01:34
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-12 14:40:38
 * @FilePath: \keep_motion_api\controllers\permission\admin.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Admin } = require('@/models/permission');
const { destroyToken } = require('@/middleWares/auth/jwt.js');

// 查询管理员
const getAdmin = async (req, res) => {
  try {
    const { username, role, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Admin.getAdmin(username, role, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询字典类型失败', error });
  }
}
// 创建管理员
const createAdmin = async (req, res) => {
  try {
    const { username, password, role, nickname } = req.body;
    const admin = await Admin.createAdmin(username, password, role, nickname);
    res.status(200).json(admin);
  } catch (error) {
    res.status(500).json({ message: '创建管理员失败', error });
  }
}
// 修改管理员
const updateAdmin = async (req, res) => {
  try {
    const { id } = req.params;
    const { username, password, role, nickname } = req.body;
    const admin = await Admin.updateAdmin(id, username, password, role, nickname);
    res.status(200).json(admin);
  } catch (error) {
    res.status(500).json({ message: '修改管理员失败', error });
  }
}
// 修改管理员状态
const updateAdminStatus = async (req, res) => {
  try {
    const { id } = req.params;
    const { status } = req.body;
    const admin = await Admin.updateAdminStatus(id, status);
    res.status(200).json(admin);
  } catch (error) {
    res.status(500).json({ message: '修改管理员状态失败', error });
  }
}

// 管理员登录
const login = async (req, res) => {
  try {
    const { userName, passWord } = req.body;
    const admin = await Admin.login(userName, passWord);
    res.status(200).send(admin);
  } catch (error) {
    res.status(500).json({ message: '登录失败', error });
  }
}

const logout = async (req, res) => {
  try {
    destroyToken(req, res);
  } catch (error) {
    res.status(500).json({ message: '退出失败', error });
  }
}


module.exports = {
  getAdmin,
  createAdmin,
  updateAdmin,
  updateAdminStatus,
  login,
  logout
}